package com.ruoyi.project.drug.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.drug.domain.DrugInbound;
import com.ruoyi.project.drug.service.DrugInboundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/drug/inbound")
public class DrugInboundController extends BaseController {
    @Autowired
    private DrugInboundService drugInboundService;

    //查询
    @GetMapping("/list")
    @PreAuthorize("@ss.hasAnyPermi('drug:inbound:list')")
    public TableDataInfo list(DrugInbound drugInbound){
        startPage();
        List<DrugInbound> list= drugInboundService.selectDrugInbound(drugInbound);
        return getDataTable(list);
    }

    //新增
    @PreAuthorize("@ss.hasPermi('drug:inbound:add')")
    @Log(title = "采购添加", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DrugInbound drugInbound){
        drugInbound.setCreateBy(SecurityUtils.getUsername());
        return toAjax(drugInboundService.insertInbound(drugInbound));
    }
    //修改
    @PreAuthorize("@ss.hasPermi('drug:inbound:query')")
    @GetMapping(value = "/{inboundId}")
    public AjaxResult getInfo(@PathVariable Long inboundId)
    {
        return AjaxResult.success(drugInboundService.selectInboundById(inboundId));
    }

    @PreAuthorize("@ss.hasPermi('drug:inbound:edit')")
    @Log(title = "采购管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody DrugInbound drugInbound)
    {

        drugInbound.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(drugInboundService.updateInbound(drugInbound));
    }

    //删除
    @PreAuthorize("@ss.hasPermi('drug:inbound:remove')")
    @Log(title = "采购删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{inboundIds}")
    public AjaxResult remove(@PathVariable Long[] inboundIds)
    {
        return toAjax(drugInboundService.deleteInboundByIds(inboundIds));
    }
}
